import {
    Component,
    Injector,
    ViewEncapsulation,
    ViewChild,
    ElementRef,
} from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    PipelineListDto,
    OrganizationUnitServiceProxy,
    ComboboxItemDto,
    BatchTaskServiceProxy,
    BatchDto,
} from '@shared/service-proxies/service-proxies';
import { PipelineNodeServiceProxy } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import { appModuleAnimation } from '@shared/animations/routerTransition';

@Component({
    selector: 'zl4',
    templateUrl: './zl4.component.html',
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()],
})
export class ZL4ModalComponent extends AppComponentBase {
    saving = false;

    pipe: PipelineListDto = new PipelineListDto();
    batch = new BatchDto();
    hgzNo = '';

    constructor(
        injector: Injector,
        private _organizationUnitServiceProxy: OrganizationUnitServiceProxy,
        private _taskServiceProxy: PipelineNodeServiceProxy,
        private _batchTaskServiceProxy: BatchTaskServiceProxy
    ) {
        super(injector);
    }

    load(pipe: PipelineListDto): void {
        this.pipe = pipe;

        this._batchTaskServiceProxy
            .getBatch(pipe.taskId)
            .subscribe((result) => {
                this.batch = result;
                this.batch.certificateQuantity=pipe.amountPlanned;
            });
    }

    save(): void {
        this.saving = true;
        this._taskServiceProxy
            .setHgzNo(this.pipe.id, this.batch.certificateId)
            .subscribe((result) => {});
        this._batchTaskServiceProxy
            .setHgz(
                this.pipe.taskId,
                this.batch.certificateId,
                this.batch.certificateQuantity
            )
            .pipe(
                finalize(() => {
                    this.saving = false;
                })
            )
            .subscribe((result) => {
                this.notify.success('记录合格证成功');
                this._batchTaskServiceProxy
                    .getBatch(this.pipe.taskId)
                    .subscribe((result) => {
                        this.batch = result;
                    });
            });
    }
}
